__author__ = """blues zhao <blues@pingpower.cn>"""
__docformat__ = 'restructuredtext'

from AccessControl import ClassSecurityInfo
from zope.interface import implements

from Products.ATContentTypes.content.base import registerATCT
from Products.ATContentTypes.content.image import ATImage
from Products.ATContentTypes.content.base import ATCTFileContent

from Products.ePub.config import PROJECTNAME
from Products.ePub.interfaces import IComixPage

from Products.ePub import ePubMessageFactory as _

class ComixPage(ATImage):
    """A page, which can be referenced in documents or displayed in an album."""

    portal_type    = 'ComixImage'
    archetype_name = 'ComixImage'
    _atct_newTypeFor = {'portal_type' : 'CMF Image', 'meta_type' : 'Comix Image'}
    assocMimetypes = ('image/*', )
    assocFileExt   = ('jpg', 'jpeg', 'png', 'gif', )
    cmf_edit_kws   = ('file', )

    implements(IComixPage)

    security       = ClassSecurityInfo()

    def __bobo_traverse__(self, REQUEST, name):
        """Transparent access to image scales
        """
        suffix = '.'+self.id
        if name.endswith(suffix):
            field = self.getField('image')
            image = None
            length = len(name) - len(suffix)
            scalename = name[:length]
            if scalename in field.getAvailableSizes(self):
                image = field.getScale(self, scale=scalename)
            if image is not None and not isinstance(image, basestring):
                # image might be None or '' for empty images
                return image

        return ATCTFileContent.__bobo_traverse__(self, REQUEST, name)

registerATCT(ComixPage, PROJECTNAME)